package com.cmcc.groupcontacts.firewall.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.cmcc.groupcontacts.firewall.adapter.BaseAdapter;
import com.yinhai.activity.R;

/**
 * 包含ListView的Activity抽象基类
 * 
 * @author 
 * 
 */
public abstract class BaseListActivity extends BaseActivity {
	/**
	 * This field should be made private, so it is hidden from the SDK. {@hide
	 * 
	 * 
	 * 
	 * 
	 * 
	 * }
	 */
	protected ListAdapter mAdapter;
	/**
	 * This field should be made private, so it is hidden from the SDK. {@hide
	 * 
	 * 
	 * 
	 * 
	 * 
	 * }
	 */
	protected ListView mList;
	protected TextView customTitle;

	private View emptyView;
	private Handler mHandler = new Handler();
	private boolean mFinishedStart = false;

	public View getEmptyView() {
		return emptyView;
	}

	public void setEmptyView(View emptyView) {
		this.emptyView = emptyView;
	}

	private Runnable mRequestFocus = new Runnable() {
		public void run() {
			mList.focusableViewAvailable(mList);
		}
	};

	/**
	 * This method will be called when an item in the list is selected. Subclasses should override. Subclasses can call
	 * getListView().getItemAtPosition(position) if they need to access the data associated with the selected item.
	 * 
	 * @param l
	 *            The ListView where the click happened
	 * @param v
	 *            The view that was clicked within the ListView
	 * @param position
	 *            The position of the view in the list
	 * @param id
	 *            The row id of the item that was clicked
	 */
	protected void onListItemClick(ListView l, View v, int position, long id) {
	}

	/**
	 * Ensures the list view has been created before Activity restores all of the view states.
	 * 
	 * @see Activity#onRestoreInstanceState(Bundle)
	 */
	@Override
	protected void onRestoreInstanceState(Bundle state) {
		super.onRestoreInstanceState(state);
	}

	/**
	 * Updates the screen state (current list and other views) when the content changes.
	 * 
	 * @see Activity#onContentChanged()
	 */
	@Override
	public void onContentChanged() {
		super.onContentChanged();
		emptyView = findViewById(android.R.id.empty);
		mList = (ListView) findViewById(android.R.id.list);
		if (mList == null) {
			throw new RuntimeException("Your content must have a ListView whose id attribute is " + "'android.R.id.list'");
		}
		if (emptyView != null) {
			mList.setEmptyView(emptyView);
		}
		mList.setOnItemClickListener(mOnClickListener);
		if (mFinishedStart) {
			setListAdapter(mAdapter);
		}
		mHandler.post(mRequestFocus);
		mFinishedStart = true;
	}

	/**
	 * Provide the cursor for the list view.
	 */
	public void setListAdapter(ListAdapter adapter) {
		synchronized (this) {
			mAdapter = adapter;
			mList.setAdapter(adapter);

			if (adapter == null || adapter.isEmpty()) {
				showEmpty();
			}
		}
	}

	/**
	 * Set the currently selected list item to the specified position with the adapter's data
	 * 
	 * @param position
	 */
	public void setSelection(int position) {
		mList.setSelection(position);
	}

	/**
	 * Get the position of the currently selected list item.
	 */
	public int getSelectedItemPosition() {
		return mList.getSelectedItemPosition();
	}

	/**
	 * Get the cursor row ID of the currently selected list item.
	 */
	public long getSelectedItemId() {
		return mList.getSelectedItemId();
	}

	/**
	 * Get the activity's list view widget.
	 */
	public ListView getListView() {
		return mList;
	}

	/**
	 * Get the ListAdapter associated with this activity's ListView.
	 */
	public ListAdapter getListAdapter() {
		return mAdapter;
	}

	/**
	 * 获取BaseAdapter
	 */
	@SuppressWarnings("rawtypes")
	public BaseAdapter getBaseAdapter() {
		return (BaseAdapter) mAdapter;
	}

	private AdapterView.OnItemClickListener mOnClickListener = new AdapterView.OnItemClickListener() {
		public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
			onListItemClick((ListView) parent, v, position, id);
		}
	};

	/**
	 * 获取数据加载中布局（用于异步加载数据的页面显示）
	 * 
	 * @return
	 */
	protected LinearLayout getLoadingLayout() {
		return (LinearLayout) findViewById(R.id.lay_loading);
	}

	/**
	 * 获取加载后无数据布局
	 * 
	 * @return
	 */
	protected LinearLayout getEmptyLayout() {
		return (LinearLayout) findViewById(R.id.lay_empty);
	}

	/**
	 * 设置无数据时的提示信息
	 * 
	 * @param emptyText
	 */
	protected void setEmptyText(String emptyText) {
		// 设置加载中的显示
		showLoading();
		// 列表有数据则清空
		if (mAdapter != null && !mAdapter.isEmpty()) {
			getBaseAdapter().getDataSource().clear();
			getBaseAdapter().notifyDataSetChanged();
		}
		// 设置无数据时的显示
		TextView txtEmpty = (TextView) findViewById(R.id.txt_empty);
		if (txtEmpty != null) {
			txtEmpty.setText(emptyText);
		}
	}

	/**
	 * 显示无数据提示信息
	 */
	protected void showEmpty() {
		getLoadingLayout().setVisibility(View.INVISIBLE);
		getEmptyLayout().setVisibility(View.VISIBLE);
	}

	/**
	 * 显示加载中（用于异步加载数据）
	 */
	protected void showLoading() {
		getLoadingLayout().setVisibility(View.VISIBLE);
		getEmptyLayout().setVisibility(View.INVISIBLE);
	}
}
